package com.xiaoyg.algorithm;

import java.util.Stack;

public class Stack2Queue {
    // 数据栈
    private Stack<Integer> stack;
    // 辅助栈
    private Stack<Integer> aux;
    Stack2Queue() {
        stack = new Stack<>();
        aux = new Stack<>();
    }
    // 入队--通过数据栈与辅助栈相互交换，保证新加入的元素沉在数据栈底
    public void enqueue(Integer e) {
        while (!stack.isEmpty()) {
            aux.push(stack.pop());
        }
        stack.push(e);
        while(!aux.isEmpty()){
            stack.push(aux.pop());
        }
    }
    // 出队--弹出数据栈元素
    public Integer dequeue(){
        return stack.pop();
    }
    // 查看队头元素
    public Integer peek(){
        return stack.peek();
    }
    // 是否为空
    public boolean isEmpty(){
        return stack.isEmpty();
    }
    public static void main(String[] args) {
        Stack2Queue queue = new Stack2Queue();
        queue.enqueue(1);
        System.out.println(queue.peek());
        queue.enqueue(2);
        System.out.println(queue.peek());
        queue.enqueue(3);
        System.out.println(queue.peek());
        System.out.println("=============");
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
    }
}
